Lecture 4 - Practice
Q1: Atoms Validation
حدد إذا كان كل كود من دول يعتبر Atom صحيح ولا لأ:
2340iehgeorge-smithVoid_alphax25x_y'Sarah Jones'<---->annaalpha__beta_procedure
Solution (Atoms Validation)
% 1) 2340ieh → False (begins with a number)
% 2) george-smith → False (contains dash '-', not a valid atom char)
% 3) Void → False (begins with capital letter)
% 4) _alpha → False (begins with underscore, treated as variable)
% 5) x25 → True (lowercase + digits)
% 6) x_y → True (lowercase + underscore)
% 7) 'Sarah Jones' → True (enclosed in single quotes)
% 8) <----> → True (string of special characters)
% 9) anna → True (lowercase letters)
% 10) alpha__beta_procedure → True (lowercase + underscores)
Q2: Structure Basics
اعمل structures للداتا دي باستخدام functors مناسبة:
- Date: 1 May 2001 → use functor
date - Person: Bob lives in London and is 48 years old → use functor
person - Address: flat 4, street 'Home Str.', postcode eh8_9lw → use functor
addr
وبعدين جرب الـ unification:
date(4, M, 2001) = date(D1, may, Y1)person(Nm, london, Age) = person(bob, london, 48)person(S, _, 45) = person(harry, dude, 45)addr(flat(4), street('Home Str.'), postcode(eh8_9lw)) = addr(flat(Z), Yy, postcode(Xxx))
Solution (Structure Basics)
% Structures
date(1, may, 2001).
person(bob, london, 48).
addr(flat(4), street('Home Str.'), postcode(eh8_9lw)).
Unification:
?- date(4, M, 2001) = date(D1, may, Y1).
% D1 = 4, M = may, Y1 = 2001
?- person(Nm, london, Age) = person(bob, london, 48).
% Nm = bob, Age = 48
?- person(S, _, 45) = person(harry, dude, 45).
% S = harry
?- addr(flat(4), street('Home Str.'), postcode(eh8_9lw)) = addr(flat(Z), Yy, postcode(Xxx)).
% Z = 4, Yy = street('Home Str.'), Xxx = eh8_9lw
Q3: Cars Database
اعمل قاعدة بيانات للعربيات باستخدام structure car(Make, Age, Price):
- Joe has a Ford car, age 3, price 5000.
- Joe has an Opel car, age 2, price 6000.
- Mona has a Toyota car, age 5, price 1000.
- Mona has a Ford car, age 2, price 2000.
واكتب الاستعلامات:
- What are the age and price of Mona's Ford car?
- Who has a Ford car?
- Display cars that have price < 5000.
Solution (Cars Database)
% Facts
has(joe, car(ford, 3, 5000)).
has(joe, car(opel, 2, 6000)).
has(mona, car(toyota, 5, 1000)).
has(mona, car(ford, 2, 2000)).
Queries:
?- has(mona, car(ford, Age, Price)).
% Age = 2, Price = 2000
?- has(Person, car(ford, _, _)).
% Person = joe ; Person = mona
?- has(_, car(Make, _, Price)), Price < 5000.
% Make = toyota, Price = 1000 ;
% Make = ford, Price = 2000
Q4: Geometric Objects
استخدم functors point, seg, triangle عشان تمثل الأشكال الهندسية دي:
- Point P1 with coordinates (1, 1).
- Point P2 with coordinates (2, 3).
- Line segment S from P1 to P2.
- Triangle T with vertices (4,2), (6,4), (7,1).
وبعدين جرب الـ matching:
triangle = trianglepoint(1,1) = XA = point(4, Y)point(2,3) = point(2, Z)
Solution (Geometric Objects)
point(1, 1).
point(2, 3).
seg(point(1, 1), point(2, 3)).
triangle(point(4, 2), point(6, 4), point(7, 1)).
Matching:
?- triangle = triangle. % true
?- point(1, 1) = X. % X = point(1, 1)
?- A = point(4, Y). % A = point(4, Y)
?- point(2, 3) = point(2, Z). % Z = 3
Q5: Tree Representation
استخدم functors عشان تمثل الـ expressions دي على شكل tree:
(a + b) * (c - 5)→ استخدم*,+,-كـ functors- Parallel circuit:
par(r1, r2) - Series circuit:
seq(r1, r2) par(r1, seq(par(r2, r3), r4))
Solution (Tree Representation)
% Arithmetic expression tree
*(+(a, b), -(c, 5)).
% Circuit trees
par(r1, r2).
seq(r1, r2).
par(r1, seq(par(r2, r3), r4)).
Q6: Vertical & Horizontal Lines
اعمل rules للخطوط:
- A line segment is vertical if its two points have the same X coordinate.
- A line segment is horizontal if its two points have the same Y coordinate.
وبعدين اكتب الاستعلامات:
- Is the segment from (1,1) to (1,2) vertical?
- Is the segment from (1,1) to (2,Y) vertical?
- Is the segment from (1,1) to (2,Y) horizontal? (اوجد Y)
- Are there any vertical segments that start at (2,3)?
Solution (Vertical & Horizontal Lines)
vertical(seg(point(X, Y), point(X, Y1))).
horizontal(seg(point(X, Y), point(X1, Y))).
Queries:
?- vertical(seg(point(1, 1), point(1, 2))). % true
?- vertical(seg(point(1, 1), point(2, Y))). % no
?- horizontal(seg(point(1, 1), point(2, Y))). % Y = 1
?- vertical(seg(point(2, 3), P)). % P = point(2, Y)
Q7: Employees Database
اعمل قاعدة بيانات للموظفين باستخدام structure employee(Name, Dept, Position, Years, Salary):
- Ahmed works in Contracts as an Engineer for 6 years with salary 8000.
- Mona works in Purchases as a Secretary for 3 years with salary 4000.
- Hany works in Stores as a Head for 8 years with salary 10000.
- Saly works in Sales as an Accountant for 4 years with salary 5000.
وبعدين اعمل rules:
- Find the department a person works in →
department/2 - Get a person's basic salary →
basic_salary/2 - Get a person's real salary →
real_salary/2 - All employees with over 5 years service get a bonus of $5000.
واكتب الاستعلامات:
- What department does Ahmed work in?
- What is Mona's basic salary?
- What is Hany's real salary?
- Who has been working for more than 5 years?
- List all employees in the Sales department.
Solution (Employees Database)
% Facts
employee(ahmed, contracts, engineer, 6, 8000).
employee(mona, purchases, secretary, 3, 4000).
employee(hany, stores, head, 8, 10000).
employee(saly, sales, accountant, 4, 5000).
% Rules
department(Person, Dept) :- employee(Person, Dept, _, _, _).
basic_salary(Person, Salary) :- employee(Person, _, _, _, Salary).
real_salary(Person, Salary) :-
employee(Person, _, _, Years, Basic),
(Years > 5 -> Bonus = 5000 ; Bonus = 0),
Salary is Basic + Bonus.
Queries:
?- department(ahmed, Dept). % Dept = contracts
?- basic_salary(mona, Salary). % Salary = 4000
?- real_salary(hany, Salary). % Salary = 15000
?- employee(Person, _, _, Years, _), Years > 5.
% Person = ahmed ; Person = hany
?- employee(Name, sales, _, _, _). % Name = saly